<template>
    <div class="preview-text" :class="{ 'word-wrap': wordWrap }">
        <pre v-if="content" ref="contentRef">{{ content }}</pre>
        <div v-else-if="loading" class="text-loading">
            <div class="loading-spinner"></div>
            <span>加载中...</span>
        </div>
        <div v-else-if="error" class="text-error">
            <div class="error-icon">⚠️</div>
            <p>{{ error }}</p>
            <el-button type="primary" @click="$emit('retry')" size="small" class="retry-btn">重试</el-button>
        </div>
        <div v-else class="empty-state">
            <div class="empty-icon">📄</div>
            <p>暂无内容</p>
        </div>
    </div>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
    content: {
        type: String,
        default: ''
    },
    loading: {
        type: Boolean,
        default: false
    },
    error: {
        type: String,
        default: ''
    },
    wordWrap: {
        type: Boolean,
        default: true
    }
})

defineEmits(['retry'])

const contentRef = ref(null)

// 监听内容变化，自动滚动到顶部
watch(() => props.content, async () => {
    // 等待DOM更新后再滚动
    await new Promise(resolve => setTimeout(resolve, 0));
    if (contentRef.value) {
        contentRef.value.scrollTop = 0;
    }
})
</script>

<style scoped>
.preview-text {
    width: 100%;
    min-height: 200px;
    text-align: left;
    white-space: pre;
    word-wrap: normal;
    background: linear-gradient(145deg, #f8f9fa, #ffffff);
    border: 1px solid #e0e6ed;
    border-radius: 10px;
    padding: 24px;
    font-family: 'SF Mono', 'Fira Code', 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
    font-size: 14px;
    line-height: 1.7;
    color: #2d3748;
    overflow-x: auto;
    box-sizing: border-box;
    flex: 1;
    margin: 0;
    position: relative;
    box-shadow:
        0 2px 4px rgba(0, 0, 0, 0.05),
        0 1px 2px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
}

.preview-text:hover {
    box-shadow:
        0 4px 8px rgba(0, 0, 0, 0.08),
        0 2px 4px rgba(0, 0, 0, 0.08);
}

.preview-text.word-wrap {
    white-space: pre-wrap;
    word-wrap: break-word;
}

.preview-text pre {
    margin: 0;
    padding: 0;
    outline: none;
    color: #2d3748;
    font-size: 14px;
    line-height: 1.7;
}

.text-loading {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 100%;
    color: #718096;
    font-size: 14px;
}

.loading-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid #e2e8f0;
    border-top: 3px solid #4299e1;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 12px;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.text-error {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 100%;
    color: #e53e3e;
    text-align: center;
    padding: 24px 0;
}

.error-icon {
    font-size: 40px;
    margin-bottom: 12px;
}

.retry-btn {
    margin-top: 16px;
    background-color: #e53e3e;
    border-color: #e53e3e;
}

.retry-btn:hover {
    background-color: #c53030;
    border-color: #c53030;
}

.empty-state {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 100%;
    color: #a0aec0;
    text-align: center;
}

.empty-icon {
    font-size: 48px;
    margin-bottom: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .preview-text {
        font-size: 13px;
        padding: 16px;
        min-height: 150px;
        border-radius: 8px;
    }

    .error-icon,
    .empty-icon {
        font-size: 36px;
    }

    .loading-spinner {
        width: 28px;
        height: 28px;
    }

    .preview-text pre {
        font-size: 13px;
        line-height: 1.6;
    }
}

/* 暗色主题适配 */
@media (prefers-color-scheme: dark) {
    .preview-text {
        background: linear-gradient(145deg, #2d3748, #1a202c);
        border-color: #4a5568;
        color: #e2e8f0;
    }

    .preview-text pre {
        color: #e2e8f0;
    }

    .text-loading {
        color: #cbd5e0;
    }

    .empty-state {
        color: #718096;
    }
}
</style>
